LOCAL_PATH := $(abspath $(call my-dir))

SOURCE_PATH := src/main/cpp/libjpeg-turbo-1.5.0
HEADER_PATH := src/main/cpp/include
JPEG_SRC := jcapimin.c jcapistd.c jccoefct.c jccolor.c \
    jcdctmgr.c jchuff.c jcinit.c jcmainct.c jcmarker.c jcmaster.c \
    jcomapi.c jcparam.c jcphuff.c jcprepct.c jcsample.c jctrans.c \
    jdapimin.c jdapistd.c jdatadst.c jdatasrc.c jdcoefct.c jdcolor.c \
    jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c \
    jdmerge.c jdphuff.c jdpostct.c jdsample.c jdtrans.c jerror.c \
    jfdctflt.c jfdctfst.c jfdctint.c jidctflt.c jidctfst.c jidctint.c \
    jidctred.c jquant1.c jquant2.c jutils.c jmemmgr.c jmemnobs.c \


# WITH_ARITH
JPEG_SRC += jaricom.c
# WITH_ARITH_DEC
JPEG_SRC += jcarith.c
JPEG_SRC += jdarith.c

TURBO_JPEG_SRC := turbojpeg.c transupp.c jdatadst-tj.c jdatasrc-tj.c turbojpeg-jni.c

include $(CLEAR_VARS)
LOCAL_MODULE := libturbojpeg

LOCAL_CFLAGS += -DHAVE_CONFIG_H -O3 -fPIE

ifeq ($(TARGET_ARCH_ABI),x86_64)
LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/$(HEADER_PATH)/x86_64
LOCAL_SIMD_SRC := jsimd_x86_64.c \
    jfdctflt-sse-64.asm \
    jccolor-sse2-64.asm \
    jcgray-sse2-64.asm \
    jchuff-sse2-64.asm \
    jcsample-sse2-64.asm \
    jdcolor-sse2-64.asm \
    jdmerge-sse2-64.asm \
    jdsample-sse2-64.asm \
    jfdctfst-sse2-64.asm \
    jfdctint-sse2-64.asm \
    jidctflt-sse2-64.asm \
    jidctfst-sse2-64.asm \
    jidctint-sse2-64.asm \
    jidctred-sse2-64.asm \
    jquantf-sse2-64.asm \
    jquanti-sse2-64.asm \


LOCAL_ASMFLAGS += -felf64 -DELF -D__x86_64__ -I$(LOCAL_PATH)/$(HEADER_PATH)/x86_64

else ifeq ($(TARGET_ARCH_ABI),x86)
LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/$(HEADER_PATH)/x86
LOCAL_SIMD_SRC := jsimd_i386.c \
    jsimdcpu.asm \
    jfdctflt-3dn.asm \
    jidctflt-3dn.asm \
    jquant-3dn.asm \
    jccolor-mmx.asm \
    jcgray-mmx.asm \
    jcsample-mmx.asm \
    jdcolor-mmx.asm \
    jdmerge-mmx.asm \
    jdsample-mmx.asm \
    jfdctfst-mmx.asm \
    jfdctint-mmx.asm \
    jidctfst-mmx.asm \
    jidctint-mmx.asm \
    jidctred-mmx.asm \
    jquant-mmx.asm \
    jfdctflt-sse.asm \
    jidctflt-sse.asm \
    jquant-sse.asm \
    jccolor-sse2.asm \
    jcgray-sse2.asm \
    jchuff-sse2.asm \
    jcsample-sse2.asm \
    jdcolor-sse2.asm \
    jdmerge-sse2.asm \
    jdsample-sse2.asm \
    jfdctfst-sse2.asm \
    jfdctint-sse2.asm \
    jidctflt-sse2.asm \
    jidctfst-sse2.asm \
    jidctint-sse2.asm \
    jidctred-sse2.asm \
    jquantf-sse2.asm \
    jquanti-sse2.asm \


LOCAL_ASMFLAGS += -felf -DELF -I$(LOCAL_PATH)/$(HEADER_PATH)/x86
LOCAL_LDLIBS += -Wl,--no-warn-shared-textrel

else ifeq ($(TARGET_ARCH_ABI),armeabi)
LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/$(HEADER_PATH)/arm
LOCAL_CFLAGS += -march=armv7-a -mfloat-abi=softfp -fprefetch-loop-arrays
LOCAL_SIMD_SRC := jsimd_arm.c \
    jsimd_arm_neon.S \


else ifeq ($(TARGET_ARCH_ABI), armeabi-v7a)
LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/$(HEADER_PATH)/arm
LOCAL_ARM_NEON := true
LOCAL_CFLAGS += -D__ARM_HAVE_NEON
LOCAL_SIMD_SRC := jsimd_arm.c \
    jsimd_arm_neon.S \


else ifeq ($(TARGET_ARCH_ABI),arm64-v8a)
LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/$(HEADER_PATH)/arm64
LOCAL_ARM_NEON := true
LOCAL_CFLAGS += -D__ARM_HAVE_NEON
LOCAL_SIMD_SRC := jsimd_arm64.c \
    jsimd_arm64_neon.S \


else ifeq ($(TARGET_ARCH_ABI),mips)
LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/$(HEADER_PATH)/mips
LOCAL_SIMD_SRC := jsimd_mips.c \
    jsimd_mips_dspr2.S \


else ifeq ($(TARGET_ARCH_ABI),mips64)
LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/$(HEADER_PATH)/mips64
LOCAL_SIMD_SRC := jsimd_mips.c \
    jsimd_mips_dspr2.S \


endif

# libsimd_la_SOURCES
LOCAL_SRC_FILES += $(addprefix $(SOURCE_PATH)/simd/,$(LOCAL_SIMD_SRC))

# libjpeg_la_SOURCES from Makefile.am
LOCAL_SRC_FILES += $(addprefix $(SOURCE_PATH)/,$(JPEG_SRC))

# libturbojpeg_la_SOURCES from Makefile.am
LOCAL_SRC_FILES += $(addprefix $(SOURCE_PATH)/,$(TURBO_JPEG_SRC))


LOCAL_C_INCLUDES +=  $(LOCAL_PATH)/$(SOURCE_PATH)/simd $(LOCAL_PATH)/$(SOURCE_PATH)

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/$(SOURCE_PATH)

include $(BUILD_SHARED_LIBRARY)
